package com.example.databasetest;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @author MAC
 */
public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    private Button createDatabase;
    private Button addData;
    private Button deleteButton;
    private Button queryButton;
    private Button replaceData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);

        createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(v -> dbHelper.getWritableDatabase());

        addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(v -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "The Da Vinci Code");
            values.put("author", "Dan Brown");
            values.put("pages", 454);
            values.put("price", 16.96);
            db.insert("Book", null, values);
            values.clear();
            values.put("name", "The Lost Symbol");
            values.put("author", "Dan Brown");
            values.put("pages", 510);
            values.put("price", 19.95);
            db.insert("Book", null, values);
        });

        deleteButton = (Button) findViewById(R.id.delete_data);
        deleteButton.setOnClickListener(v -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            db.delete("Book", "pages > ?", new String[]{"500"});
        });

        queryButton = (Button) findViewById(R.id.query_data);
        queryButton.setOnClickListener(v -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query("Book", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String author = cursor.getString(cursor.getColumnIndex("author"));
                    int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                    double price = cursor.getDouble(cursor.getColumnIndex("price"));
                    Log.d("MainActivity", "book name is " + name);
                    Log.d("MainActivity", "book author is " + author);
                    Log.d("MainActivity", "book pages is " + pages);
                    Log.d("MainActivity", "book price is " + price);
                } while (cursor.moveToNext());
                cursor.close();
            }
        });

        replaceData = (Button) findViewById(R.id.replace_data);
        replaceData.setOnClickListener(v -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            //开始事务
            db.beginTransaction();
            try {
                db.delete("Book", null, null);
//                if (true) {
//                    //这里手动抛出异常，另事务失败
//                    throw new NullPointerException();
//                }
                ContentValues values = new ContentValues();
                values.put("name", "Game of Thrones");
                values.put("author", "George Martin");
                values.put("pages", 720);
                values.put("price", 20.85);
                db.insert("Book", null, values);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                db.endTransaction();
            }
        });
    }
}